import java.util.Arrays;
import java.util.Comparator;

/**
 * @author LKQ
 * @date 2022/2/21 14:33
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[][] people = {{7, 0}, {4, 4}, {7,1}, {5, 0}, {6, 1}, {5, 2}};
        solution.reconstructQueue(people);
    }
    public int[][] reconstructQueue(int[][] people) {
        Arrays.sort(people, new Comparator<int[]>() {
            @Override
            public int compare(int[] person1, int[] person2) {
                if (person1[0] != person2[0]) {
                    // 身高不同，按身高升序排列
                    return person1[0] - person2[0];
                }else {
                    // 身高相同，按人数降序排列
                    return person2[1] - person1[1];
                }
            }
        });
        int n = people.length;
        int[][] ans = new int[n][];
        for (int[] person: people) {
            int space = person[1] + 1;
            for (int i = 0; i < n; i++) {
                if (ans[i] == null) {
                    --space;
                    if (space == 0) {
                        ans[i] = person;
                        break;
                    }
                }
            }
        }
        return ans;
    }
}
